from . import api_blueprint
from app.auth.session_check import SessionCheck
from app.auth.user_auth import UserAuth
from app.model.course import Course
from flask import json


@api_blueprint.route('/login')
def hello():
    identity = UserAuth.check("admin", "12")
    if identity > -1:
        SessionCheck.set_login(identity)
        return "success!"
    return "failed!"


@api_blueprint.route('/get')
def get_user_type():
    return str(UserAuth.get_user_type_by_id("admin"))


@api_blueprint.route('/check')
def set_session():
    if SessionCheck.check_login():
        return "Welcome back!"
    return "New user."


@api_blueprint.route('/get_identity')
def get_session():
    return str(SessionCheck.get_identity())


@api_blueprint.route('/get_course')
def get_course():
    courses = Course.query.all()
    print(courses)
    return json.dumps(courses, default=Course.to_json)

